package com.FengChaoSystem.servlet;

import com.FengChaoSystem.pojo.Message;
import com.FengChaoSystem.pojo.Order;
import com.FengChaoSystem.service.MessageImpl;
import com.FengChaoSystem.service.MessageService.MessageService;
import com.FengChaoSystem.untils.DFASensitiveWordFilter;

import com.alibaba.fastjson.JSON;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.websocket.Session;
import java.io.BufferedReader;
import java.util.Arrays;
import java.util.List;

@WebServlet("/Message/*")
public class MessageServlet extends BaseServlet{
    private MessageImpl service=new MessageService();
    public List<String> sensitiveWords = Arrays.asList("我尼玛", "我操", "rnm","去死");
    public DFASensitiveWordFilter filter = new DFASensitiveWordFilter();

    public void getMessage(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String fromId=  request.getParameter("fromId");
        String toId=request.getParameter("toId");
        int fromUserId=Integer.parseInt(fromId);
        int toUserId=Integer.parseInt(toId);

        Message message=new Message();
        message.setHasRead(0);
        message.setFromUserId(fromUserId);
        message.setToUserId(toUserId);
        List<Message> messages = service.getMessage(message);


        if(!messages.isEmpty()){

        String jsonString = JSON.toJSONString(messages);
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
        service.updateHasRead(message);
        }
        else{
            response.getWriter().write("null");
        }
    }
    public void addMessage(HttpServletRequest request, HttpServletResponse response) throws Exception {

        filter.buildSensitiveWordTree(sensitiveWords);

        String fromId=  request.getParameter("fromId");
        String toId=request.getParameter("toId");
        int fromUserId=Integer.parseInt(fromId);
        int toUserId=Integer.parseInt(toId);
        BufferedReader reader = request.getReader();
        String param = reader.readLine();
        Message message= JSON.parseObject(param, Message.class);
        //获取 消息接收方的用户名
        message.setToUserId(fromUserId);
        message.setFromUserId(toUserId);
        //获取消息接收方用户对象的session对象

        String context = message.getContext();
        String filteredText = filter.filter(context);
        message.setContext(filteredText);
        service.addMessage(message);
    }

    public void selectAllHasReadMessages(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String fromId=  request.getParameter("fromId");
        String toId=request.getParameter("toId");
        int fromUserId=Integer.parseInt(fromId);
        int toUserId=Integer.parseInt(toId);

        Message message=new Message();
        message.setHasRead(0);
        message.setFromUserId(fromUserId);
        message.setToUserId(toUserId);
        List<Message> messages = service.selectAllHasReadMessages(message);


        if(!messages.isEmpty()){

            String jsonString = JSON.toJSONString(messages);
            response.setContentType("text/json;charset=utf-8");
            response.getWriter().write(jsonString);
            service.updateHasRead(message);
        }
        else{
            response.getWriter().write("null");
        }
    }




    }
